#!/usr/bin/env python
import sys
from Modules.Pystalt import PackageManager
from Modules.Pygmund import Id

__version__ = "1.0-0"
__copyright__ = "2010, G. John Schutte IV, SnohomaNET (www.snohoma.net)"
__doc__ = """\
Pystalt Application Manager
	WIKI: http://cognition-app-framework.googlecode.com
	Developer: SnohomaNET
	
Description:
	Manages the installation, uninstallation, creation and updating of
	Cognition Application Packages.
	
Usage:
	$ ./pystalt.py create application
	$ ./pystalt.py install|update|uninstall application
	$ ./pystalt.py build application
	$ ./pystalt.py help
	$ ./pystalt.py list [application]
	
Examples:
	$ ./pystalt.py list
	$ ./pystalt.py get http://www.url.com/app.json
	$ ./pystalt.py create example
	$ ./pystalt.py build example
	$ ./pystalt.py inspect example
	$ ./pystalt.py install example
	$ ./pystalt.py list example
	$ ./pystalt.py update example
	$ ./pystalt.py uninstall example
"""
help = __doc__

def main(command="help", app=False):
	p = PackageManager.Init(Id.Init("id/cognition.conf").Get("PYSTALT"))
	
	if command == "list":
		return p.List(app)
	
	elif command == "get":
		return p.Get(app)
		
	elif command == "create":
		return p.Create(app)
		
	elif command == "build":
		return p.Build(app)
		
	elif command == "inspect":
		return p.Inspect(app)
		
	elif command == "install":
		return p.Install(app)
		
	elif command == "update":
		return p.Update(app)
		
	elif command == "uninstall":
		return p.Uninstall(app)
		
	else:
		return (True, help)
	
	
if len(sys.argv) < 2:
	print help
else:
	if len(sys.argv) < 3:
		c = sys.argv[1]
		a = False
	else:
		c = sys.argv[1]
		a = sys.argv[2]
	r = main(c, a)
	if not r[0]:
		print "Error: " + r[1]
	else:
		print r[1]
